<template>
  <a-modal
    title="采集设备网关参数配置"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="通道号生成基础数"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input-number
            style="width: 100%"
            placeholder="请输入通道号生成基础数"
            v-decorator="['engineHttpUrl', { rules: [{ required: true, message: '请输入网关设备IP！' }] }]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { edit, getConfig } from '@/api/modular/main/bizgateway/bizGatewayManage'
import { list } from '@/api/modular/main/bizstation/bizStationManage'
import { gatewayTypelist } from '@/api/modular/main/bizgatewaytype/bizGatewayTypeManage'
import { sysDictTypeDropDown } from '@/api/modular/system/dictManage'
import { listScene } from '@/api/modular/main/gatewayscene/gatewayscene'

export default {
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 },
      },
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      stationTree: [],
      gatewayTypeList: [],
      treeLoading: true,
      groupCodeList: [],
      sceneList: [],
      gatewayId: 0,
      record: [],
    }
  },
  methods: {
    toIndex(record) {
      this.gatewayId = record.id
      this.visible = true
      this.getStationTree()
      this.sysDictTypeDropDown()
      this.getSceneList()
      this.initFormData(() => {
        this.form.setFieldsValue({
          router_number: record.router_number,
          server_address: record.server_address,
          micGroupId: record.micGroupId,
          micAccessToken: record.micAccessToken,
          micRouterBase: record.micRouterBase,
          micDeviceType: record.micDeviceType,
          micIsShieldDefaultDevice: record.micIsShieldDefaultDevice,
          micTcpPort: record.micTcpPort,
          micDeviceName: record.micDeviceName,
          micDiskCapacity: record.micDiskCapacity,
          micUsbMode: record.micUsbMode,
          micFileStorageMode: record.micFileStorageMode,
          micMaxFrequencyVoiceRatio: record.micMaxFrequencyVoiceRatio,
          micNoFrequencyVoiceRatio: record.micNoFrequencyVoiceRatio,
          micUsbModeChannelNum: record.micUsbModeChannelNum,
          micUploadFileQuantity: record.micUploadFileQuantity,
          micFileErrorIsDelete: record.micFileErrorIsDelete,
          audioWavSize: record.audioWavSize,
          audioScanPath: record.audioScanPath,
          audioTmpSavePath: record.audioTmpSavePath,
          audioBackupPath: record.audioBackupPath,
          engineHttpUrl: record.engineHttpUrl,
          engineFileSaveUrl: record.engineFileSaveUrl,
          sensorOnOrOff: record.sensorOnOrOff,
          sensorSerialCom: record.sensorSerialCom,
          sensorSensorSampleRate: record.sensorSensorSampleRate,
          audiocaptureOnOrOff: record.audiocaptureOnOrOff,
          audiocaptureSerialName: record.audiocaptureSerialName,
          audiocaptureRecordingTime: record.audiocaptureRecordingTime,
          soundmakerOnOrOff: record.soundmakerOnOrOff,
          soundmakerFrequency: record.soundmakerFrequency,
          soundmakerVolumeActiveVoice: record.soundmakerVolumeActiveVoice,
          soundmakerWorkingTime: record.soundmakerWorkingTime,
          soundmakerIntermittentTime: record.soundmakerIntermittentTime,
          soundmakerPlayVoiceDeviceName: record.soundmakerPlayVoiceDeviceName,
          appNetworkInterface: record.appNetworkInterface,
          appReportStartupStatus: record.appReportStartupStatus,
          appUploadTemperatureOnOff: record.appUploadTemperatureOnOff,
          uploadNormalStatus: record.uploadNormalStatus,
        })
        console.log(this.form.appUploadTemperatureOnOff )
      })
    },
    initFormData (callback) {
      setTimeout(() => {
        getConfig({
          id: this.gatewayId,
        }).then((res) => {
          if (res.success) {
            this.record = res.data
            callback()
          }
        })
      })
    },
    initFormData2(id) {
      setTimeout(() => {
        getConfig({
          id: id,
        }).then((res) => {
          if (res.success) {
            this.record = res.data
            this.form.setFieldsValue({
              id: id,
              micGroupId: res.data.micGroupId,
              micAccessToken: res.data.micAccessToken,
              micRouterBase: res.data.micRouterBase,
              micDeviceType: res.data.micDeviceType,
              micIsShieldDefaultDevice: res.data.micIsShieldDefaultDevice,
              micTcpPort: res.data.micTcpPort,
              micDeviceName: res.data.micDeviceName,
              micDiskCapacity: res.data.micDiskCapacity,
              micUsbMode: res.data.micUsbMode,
              micFileStorageMode: res.data.micFileStorageMode,
              micMaxFrequencyVoiceRatio: res.data.micMaxFrequencyVoiceRatio,
              micNoFrequencyVoiceRatio: res.data.micNoFrequencyVoiceRatio,
              micUsbModeChannelNum: res.data.micUsbModeChannelNum,
              micUploadFileQuantity: res.data.micUploadFileQuantity,
              micFileErrorIsDelete: res.data.micFileErrorIsDelete,
              audioWavSize: res.data.audioWavSize,
              audioScanPath: res.data.audioScanPath,
              audioTmpSavePath: res.data.audioTmpSavePath,
              audioBackupPath: res.data.audioBackupPath,
              engineHttpUrl: res.data.engineHttpUrl,
              engineFileSaveUrl: res.data.engineFileSaveUrl,
              sensorOnOrOff: res.data.sensorOnOrOff,
              sensorSerialCom: res.data.sensorSerialCom,
              sensorSensorSampleRate: res.data.sensorSensorSampleRate,
              audiocaptureOnOrOff: res.data.audiocaptureOnOrOff,
              audiocaptureSerialName: res.data.audiocaptureSerialName,
              audiocaptureRecordingTime: res.data.audiocaptureRecordingTime,
              soundmakerOnOrOff: res.data.soundmakerOnOrOff,
              soundmakerFrequency: res.data.soundmakerFrequency,
              soundmakerVolumeActiveVoice: res.data.soundmakerVolumeActiveVoice,
              soundmakerWorkingTime: res.data.soundmakerWorkingTime,
              soundmakerIntermittentTime: res.data.soundmakerIntermittentTime,
              soundmakerPlayVoiceDeviceName: res.data.soundmakerPlayVoiceDeviceName,
              appNetworkInterface: res.data.appNetworkInterface,
              appReportStartupStatus: res.data.appReportStartupStatus,
              appUploadTemperatureOnOff: res.data.appUploadTemperatureOnOff,
            })
          }
        })
      }, 100)
    },
    getStationTree() {
      list().then((res) => {
        this.treeLoading = false
        if (!res.success) {
          return
        }
        this.stationTree = res.data
      })
      gatewayTypelist().then((res) => {
        this.treeLoading = false
        if (!res.success) {
          return
        }
        this.gatewayTypeList = res.data
      })
    },
    getSceneList() {
      listScene().then((res) => {
        this.sceneList = res.data
      })
    },
    sysDictTypeDropDown() {
      sysDictTypeDropDown({ code: 'gateway_config_scene' }).then((res) => {
        if (res.success) {
          this.groupCodeList = res.data
        }
      })
    },
    handleSubmit() {
      const {
        form: { validateFields },
      } = this
      this.confirmLoading = true

      validateFields((errors, values) => {
        if (!errors) {
          edit(values)
            .then((res) => {
              this.confirmLoading = false
              if (res.success) {
                this.$message.success('编辑成功')
                this.handleCancel()
                this.$emit('ok', values)
              } else {
                this.$message.info('编辑失败：' + res.message)
              }
            })
            .finally((res) => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    },
  },
}
</script>
